##########
# R Info #
##########
version
#platform       x86_64-w64-mingw32          
#arch           x86_64                      
#os             mingw32                     
#system         x86_64, mingw32             
#status                                     
#major          3                           
#minor          6.0                         
#year           2019                        
#month          04                          
#day            26                          
#svn rev        76424                       
#language       R                           
#version.string R version 3.6.0 (2019-04-26)
#nickname       Planting of a Tree 

library(ggplot2)
library(ggpubr)
library(maps)
library(wesanderson)
library(stargazer)
#library(dplyr)

sessionInfo()
#R version 3.6.0 (2019-04-26)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 10 x64 (build 19041)
#Matrix products: default
#locale:
#[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
#[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
#[5] LC_TIME=English_United States.1252    
#attached base packages:
#[1] stats     graphics  grDevices utils     datasets  methods   base     
#other attached packages:
#[1] dplyr_1.0.4       stargazer_5.2.2   wesanderson_0.3.6 maps_3.3.0        ggpubr_0.4.0     
#[6] ggplot2_3.3.2  
#loaded via a namespace (and not attached):
#[1] zip_2.0.4         Rcpp_1.0.3        cellranger_1.1.0  pillar_1.4.3      compiler_3.6.0   
#[6] forcats_0.5.0     tools_3.6.0       lifecycle_0.2.0   tibble_3.0.4      gtable_0.3.0     
#[11] pkgconfig_2.0.3   rlang_0.4.10      openxlsx_4.1.4    DBI_1.1.0         rstudioapi_0.11  
#[16] curl_4.3          haven_2.2.0       rio_0.5.16        rJava_0.9-11      withr_2.4.1      
#[21] dplyr_1.0.4       hms_0.5.3         generics_0.0.2    xlsxjars_0.6.1    vctrs_0.3.6      
#[26] grid_3.6.0        tidyselect_1.1.0  data.table_1.12.8 glue_1.4.1        R6_2.4.1         
#[31] rstatix_0.6.0     readxl_1.3.1      foreign_0.8-71    carData_3.0-3     car_3.0-7        
#[36] purrr_0.3.3       tidyr_1.1.2       magrittr_1.5      backports_1.1.5   scales_1.1.0     
#[41] ellipsis_0.3.0    abind_1.4-5       assertthat_0.2.1  colorspace_1.4-1  ggsignif_0.6.0   
#[46] xlsx_0.6.1        stringi_1.4.5     munsell_0.5.0     broom_0.7.5       crayon_1.3.4




#############
# Load Data #
#############
load("PostDataJQD.RData")

colnames(df)
# country:          country name
# year:             year of posting
# yearmonth:        year + month of posting
# party_country:    party name + country name
# party:            party name
# cmp_familyid:     party family id in the CMP
# cmp_family:       party family name in the CMP
# ches_ideology:    general left-right ideology in the CHES
# gps_populism:     populism score in the GPS
# gps_econlr:       economic left-right ideology in the GPS      
# gps_soclr:        social left-right ideology in the GPS
# Page.Name:        name of facebook page
# Likes.at.Posting: number of page likes at posting
# Likes:            number of Likes
# Comments:         number of comments
# Shares:           number of shares
# Love:             number of Love reactions
# Wow:              number of Wow reactions
# Haha:             number of Haha reactions 
# Sad:              number of Sad reactions
# Angry:            number of Angry reactions
# totalreactions:   sum of Likes, Love, Wow, Haha, Sad, and Angry
# loveprop:         proportion of Love reactions (Love / totalreactions)
# angryprop:        proportion of Angry reactions (Angry / totalreactions)

summary(df)

# number of countries
length(unique(df$country)) # 79

# number of parties
length(unique(df$party_country)) # 690




###################################
# Table 1: Descriptive Statistics #
###################################
# proportion like
df$likeprop <- df$Likes / df$totalreactions
df$likeprop[df$totalreactions == 0] <- 0  

# proportion wow
df$wowprop <- df$Wow / df$totalreactions
df$wowprop[df$totalreactions == 0] <- 0

# proportion haha
df$hahaprop <- df$Haha / df$totalreactions
df$hahaprop[df$totalreactions == 0] <- 0

# proportion sad
df$sadprop <- df$Sad / df$totalreactions
df$sadprop[df$totalreactions == 0] <- 0

# emotional polarization
df$lapolarization <- (df$angryprop + 1) / (df$loveprop + 1)

# table 1
stargazer(df[,c("Comments", "Shares", "totalreactions", 
                "Likes", "Love", "Angry", "Wow", "Haha", "Sad",
                "likeprop", "loveprop", "angryprop",
                "wowprop", "hahaprop", "sadprop", "lapolarization")],
          covariate.labels=c("# Comments", "# Shares", "# Total Reactions",
                             "# Likes", "# Love", "# Angry", 
                             "# Wow", "# Haha", "# Sad",
                             "Like Proportion", "Love Proportion", "Angry Proportion",
                             "Wow Proportion", "Haha Proportion", "Sad Proportion",
                             "Emotional Polarization"),
          omit.summary.stat=c("p25","p75", "min", "N"),  digits=2)
